package com.cn.view;

import com.cn.bean.Department;
import com.cn.bean.Worker;
import com.cn.dao.DepartmentDao;
import com.cn.dao.WorkerDao;
import com.cn.util.StringUtils;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * @Author Zhou Tao
 * @Date 2023/12/6 19:38
 */
public class WorkerAddFrame extends JInternalFrame {
    private JTextField nameTextField;
    private JTextField snTextField;
    private final ButtonGroup buttonGroup = new ButtonGroup();
    private JTextField deptTextField;
    private JComboBox departmentComboBox;
    private JTextArea addressTextArea;
    private JRadioButton maleRadioButton;
    private JRadioButton femaleRadioButton;

    /**
     * Create the frame.
     */
    public WorkerAddFrame() {
        setClosable(true);
        setIconifiable(true);
        setTitle("添加员工界面");
        setBounds(100, 100, 450, 467);
        JLabel label = new JLabel("员工姓名");
        nameTextField = new JTextField();
        nameTextField.setColumns(10);
        JLabel label_1 = new JLabel("员工学号");
        snTextField = new JTextField();
        snTextField.setColumns(10);
        JLabel label_2 = new JLabel("员工性别");
        maleRadioButton = new JRadioButton("男");
        buttonGroup.add(maleRadioButton);
        maleRadioButton.setSelected(true);
        femaleRadioButton = new JRadioButton("女");
        buttonGroup.add(femaleRadioButton);
        JLabel label_3 = new JLabel("所在部门岗位");
        deptTextField = new JTextField();
        deptTextField.setColumns(10);
        JLabel label_4 = new JLabel("家庭住址");
        addressTextArea = new JTextArea();
        JLabel label_5 = new JLabel("部门名称");
        departmentComboBox = new JComboBox();

        // 点击"添加"按钮执行add()方法
        JButton addButton = new JButton("添加");
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 获取员工姓名
                String name=nameTextField.getText();
                // 获取员工工号
                String sn=snTextField.getText();
                // 获取员工所在部门
                String dept=deptTextField.getText();
                // 获取员工家庭住址
                String address=addressTextArea.getText();

                if(StringUtils.isEmpty(name)){
                    JOptionPane.showMessageDialog(null, "员工姓名不能为空！");
                    return;
                }

                if(StringUtils.isEmpty(sn)){
                    JOptionPane.showMessageDialog(null, "员工工号不能为空！");
                    return;
                }

                if(StringUtils.isEmpty(dept)){
                    JOptionPane.showMessageDialog(null, "员工所在部门岗位不能为空！");
                    return;
                }

                // 获取员工性别
                String sex="";
                if(maleRadioButton.isSelected()){
                    sex="男";
                }else if(femaleRadioButton.isSelected()){
                    sex="女";
                }
                String studentSex = sex;

                // 获取所在部门的ID
                Department department=(Department) departmentComboBox.getSelectedItem();
                int departmentID=department.getDeptId();

                // 添加员工
                add(name, sn,studentSex, dept, departmentID,address);
            }
        });
        addButton.setIcon(new ImageIcon(WorkerAddFrame.class.getResource("/images/add.png")));

        // 点击"重置"按钮执行reset()方法
        JButton resetButton = new JButton("重置");
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                reset();
            }
        });
        resetButton.setIcon(new ImageIcon(WorkerAddFrame.class.getResource("/images/reset.png")));
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        GroupLayout.ParallelGroup parallelGroup1 = groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        GroupLayout.SequentialGroup sequentialGroup1 = groupLayout.createSequentialGroup();
        sequentialGroup1.addGap(42);
        GroupLayout.ParallelGroup parallelGroup2 = groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        GroupLayout.SequentialGroup sequentialGroup2 = groupLayout.createSequentialGroup();
        sequentialGroup2.addComponent(addButton);
        sequentialGroup2.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED);
        sequentialGroup2.addComponent(resetButton);
        sequentialGroup2.addGap(232);
        parallelGroup2.addGroup(sequentialGroup2);
        GroupLayout.ParallelGroup parallelGroup3 = groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        parallelGroup3.addComponent(label_5);
        GroupLayout.SequentialGroup sequentialGroup3 = groupLayout.createSequentialGroup();
        GroupLayout.ParallelGroup parallelGroup4 = groupLayout.createParallelGroup(GroupLayout.Alignment.TRAILING);
        parallelGroup4.addComponent(label_4);
        parallelGroup4.addComponent(label_2);
        parallelGroup4.addComponent(label);
        sequentialGroup3.addGroup(parallelGroup4);
        sequentialGroup3.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED);
        GroupLayout.ParallelGroup parallelGroup5 = groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        GroupLayout.SequentialGroup sequentialGroup4 = groupLayout.createSequentialGroup();
        GroupLayout.ParallelGroup parallelGroup6 = groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false);
        parallelGroup6.addComponent(nameTextField, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE);
        GroupLayout.SequentialGroup sequentialGroup5 = groupLayout.createSequentialGroup();
        sequentialGroup5.addComponent(maleRadioButton);
        sequentialGroup5.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED);
        sequentialGroup5.addComponent(femaleRadioButton);
        parallelGroup6.addGroup(sequentialGroup5);
        parallelGroup6.addComponent(departmentComboBox, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE);
        sequentialGroup4.addGroup(parallelGroup6);
        sequentialGroup4.addGap(35);
        GroupLayout.ParallelGroup parallelGroup7 = groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false);
        GroupLayout.SequentialGroup sequentialGroup6 = groupLayout.createSequentialGroup();
        sequentialGroup6.addComponent(label_1);
        sequentialGroup6.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED);
        sequentialGroup6.addComponent(snTextField, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE);
        parallelGroup7.addGroup(sequentialGroup6);
        GroupLayout.SequentialGroup sequentialGroup7 = groupLayout.createSequentialGroup();
        sequentialGroup7.addComponent(label_3);
        sequentialGroup7.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED);
        sequentialGroup7.addComponent(deptTextField);
        parallelGroup7.addGroup(sequentialGroup7);
        sequentialGroup4.addGroup(parallelGroup7);
        parallelGroup5.addGroup(sequentialGroup4);
        parallelGroup5.addComponent(addressTextArea);
        sequentialGroup3.addGroup(parallelGroup5);
        sequentialGroup3.addContainerGap(44, Short.MAX_VALUE);
        parallelGroup3.addGroup(sequentialGroup3);
        parallelGroup2.addGroup(parallelGroup3);
        sequentialGroup1.addGroup(parallelGroup2);
        parallelGroup1.addGroup(sequentialGroup1);
        groupLayout.setHorizontalGroup(parallelGroup1);
        GroupLayout.ParallelGroup group1 = groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        GroupLayout.SequentialGroup sGroup1 = groupLayout.createSequentialGroup();
        sGroup1.addGap(42);
        GroupLayout.ParallelGroup group2 = groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);
        group2.addComponent(label);
        group2.addComponent(nameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE);
        group2.addComponent(label_1);
        group2.addComponent(snTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE);
        sGroup1.addGroup(group2);
        sGroup1.addGap(29);
        GroupLayout.ParallelGroup group3 = groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);
        group3.addComponent(label_2);
        group3.addComponent(maleRadioButton);
        group3.addComponent(femaleRadioButton);
        group3.addComponent(label_3);
        group3.addComponent(deptTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE);
        sGroup1.addGroup(group3);
        sGroup1.addGap(33);
        GroupLayout.ParallelGroup group4 = groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);
        group4.addComponent(label_5);
        group4.addComponent(departmentComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE);
        sGroup1.addGroup(group4);
        sGroup1.addGap(30);
        GroupLayout.ParallelGroup group5 = groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        group5.addComponent(label_4);
        group5.addComponent(addressTextArea, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE);
        sGroup1.addGroup(group5);
        sGroup1.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE);
        GroupLayout.ParallelGroup group6 = groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);
        group6.addComponent(addButton);
        group6.addComponent(resetButton);
        sGroup1.addGroup(group6);
        sGroup1.addGap(42);
        group1.addGroup(sGroup1);
        groupLayout.setVerticalGroup(group1);
        getContentPane().setLayout(groupLayout);
        addressTextArea.setBorder(new LineBorder(new java.awt.Color(127,157,185), 1, false));
        // 初始所在部门的下拉列表
        initSchoolClass();
    }

    /**
     * core code
     * 初始化部门的下拉列表
     */
    private void initSchoolClass(){
        // 利用DepartmentDao查询所有的部门
        DepartmentDao departmentDao=new DepartmentDao();
        ArrayList<Department> list=departmentDao.query(null);
        // 将各个部门均添加至下拉列表
        for(int i=0;i<list.size();i++){
            Department dept = list.get(i);
            this.departmentComboBox.addItem(dept);
        }
    }

    /**
     * core code
     * 添加员工
     */
    private void add(String name,String sn,String sex,String dept,int departmentID,String address) {
        // 利用WorkerDao添加员工
        WorkerDao workerDao = new WorkerDao();
        Worker worker = new Worker(name, sn, sex, dept, departmentID, address);
        int result = workerDao.add(worker);
        if(result==1){
            JOptionPane.showMessageDialog(null, "员工添加成功！");
            reset();
        }else{
            System.out.println(worker.getWorkerName() + worker.getWorkerNumber() + worker.getWorkerSex() + worker.getDept() + worker.getDepartmentID() + worker.getWorkerAddress());
            JOptionPane.showMessageDialog(null, "员工添加失败！");
        }

    }

    /**
     * core code
     * 重置
     */
    private void reset(){
        this.nameTextField.setText("");
        this.snTextField.setText("");
        this.deptTextField.setText("");
        this.maleRadioButton.setSelected(true);
        this.addressTextArea.setText("");
        if(this.departmentComboBox.getItemCount()>0){
            this.departmentComboBox.setSelectedIndex(0);
        }
    }
}
